package com.itheima.sh.response;

import java.io.Serializable;

/**
 * 通用的结果返回类
 * @param <T>
 */
public class Result<T> implements Serializable {

    private String host="localhost";  // 访问主机IP

    private Integer code;  // 状态码

    private String message; // 提示信息：错误信息

    private T data; // 封装的数据

    public Result() {
        this.code = 200;
    }

    public Result(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.message = msg;
    }

    public static Result errorResult(int code, String msg) {
        Result result = new Result();
        return result.error(code, msg);
    }

    public static Result okResult(int code, String msg) {
        Result result = new Result();
        return result.ok(code, null, msg);
    }

    public static Result okResult(Object data) {
        Result result = setStatusCode(StatusCode.SUCCESS, StatusCode.SUCCESS.getMessage());
        if(data!=null) {
            result.setData(data);
        }
        return result;
    }

    public static Result errorResult(StatusCode enums){
        return setStatusCode(enums,enums.getMessage());
    }

    public static Result errorResult(StatusCode enums, String message){
        return setStatusCode(enums,message);
    }

    public static Result setStatusCode(StatusCode enums){
        return okResult(enums.getCode(),enums.getMessage());
    }

    private static Result setStatusCode(StatusCode enums, String message){
        return okResult(enums.getCode(),message);
    }

    public static Result result(boolean b) {
        return okResult(b ? StatusCode.SUCCESS.getCode() : StatusCode.ERROR.getCode(),
                b ? StatusCode.SUCCESS.getMessage() : StatusCode.ERROR.getMessage());
    }

    public Result<?> error(Integer code, String msg) {
        this.code = code;
        this.message = msg;
        return this;
    }

    public Result<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public Result<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.message = msg;
        return this;
    }

    public Result<?> ok(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }
}
